from datetime import datetime

from sqlmodel import SQLModel, Field, Relationship

from models.relationships.role_permission import RolePermissionLink
from models.relationships.user_role import UserRoleLink
from models.permission import Permission
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from models.user import User


class Role(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True)
    name: str = Field(max_length=30, unique=True, index=True)
    description: str = Field(max_length=100)
    created_at: datetime = Field(default_factory=datetime.now)
    users: list["User"] = Relationship(back_populates="roles", link_model=UserRoleLink)
    permissions: list[Permission] = Relationship(back_populates="roles", link_model=RolePermissionLink)
